# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        # if not pHead:
        #     return pHead
        # res = ListNode(pHead.val)
        # while pHead.next:
        #     pHead = pHead.next
        #     cur = ListNode(pHead.val)
        #     cur.next = res
        #     res = cur
        # return res
        # 不使用额外的空间
        if not pHead or not pHead.next:
            return pHead
        last = None
        while pHead:
            tmp = pHead.next
            pHead.next = last
            last = pHead
            pHead = tmp
        return last